import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;


public class Connection  extends Thread{
	private Socket client = null;
	private OutputStreamWriter oos = null;
	private BufferedReader in = null;
	public Connection() {
	}
	public Connection(Socket clientSocket) {
		client = clientSocket;
		try {
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			oos = new OutputStreamWriter(client.getOutputStream());
		} catch (Exception e1) {
			System.out.println(e1);
			try {
				client.close();
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			return;
		}
		this.start();
	}
	public void run() {
		String line;
		while (!client.isClosed()) {
			try {
				line = in.readLine();
				System.out.println(line);
				if ("sendme".equals(line)) {
					sendMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rows><row><col1>1</col1><col2>8</col2><col3>5</col3></row><row><col1>4</col1><col2>15</col2><col3>30</col3></row></rows>");
				}
				if ("offline".equals(line)) {
					kill();
				}
			} catch (IOException e) {
				System.out.println("Read failed");
				System.exit(-1);
			}
		}
	}
	public void sendMessage(String msg) {
		try {
			oos.write(msg);
			oos.flush();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	public void kill() throws IOException {
		client.close();
	}
}
